﻿package view.popup
{
    import com.core.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import view.mornExtends.*;
    import vo.*;

    public class HuoDeBoxUI extends View implements IDialogUI
    {
        private var _data:Object;
        public var tips:UrlImage;
        public var btn_VIP:Button;
        public var item:FlashItem;
        public var closeBtn:Button;
        protected var uiXML:XML;
        private var _timeID:uint = 0;
        private var _readtime:int = 5;
        private var _isvip:Boolean = false;

        public function HuoDeBoxUI()
        {
            this._data = new Object();
            this.uiXML = <View>r
n	t	t	t  <UrlImage url=""skill/bg1.png"" y=""0""/>r
n	t	t	t  <UrlImage url=""skill/iconBG.png"" x=""104"" y=""42"" mouseEnabled=""false""/>r
n	t	t	t  <UrlImage url=""skill/vip.png"" x=""63"" y=""3"" var=""tips"" mouseEnabled=""false""/>r
n	t	t	t  <Button skin=""png.common.btn_cs2_bag"" x=""106"" y=""124"" label=""使用"" labelStroke=""1"" labelFont=""SimSun"" labelColors=""0xb29a6c,0xb29a6c,0xfff7af"" var=""btn_VIP""/>r
n	t	t	t  <FlashItem x=""120"" y=""57"" var=""item"">r
n	t	t	t	t<Image url=""png.common.noramlicon"" width=""48"" height=""48"" name=""icon""/>r
n	t	t	t  </FlashItem>r
n	t	t	t  <Button skin=""png.common.btn_cs2_close"" x=""264"" y=""6"" var=""closeBtn""/>r
n	t	t	t</View>")("<View>
			  <UrlImage url="skill/bg1.png" y="0"/>
			  <UrlImage url="skill/iconBG.png" x="104" y="42" mouseEnabled="false"/>
			  <UrlImage url="skill/vip.png" x="63" y="3" var="tips" mouseEnabled="false"/>
			  <Button skin="png.common.btn_cs2_bag" x="106" y="124" label="使用" labelStroke="1" labelFont="SimSun" labelColors="0xb29a6c,0xb29a6c,0xfff7af" var="btn_VIP"/>
			  <FlashItem x="120" y="57" var="item">
				<Image url="png.common.noramlicon" width="48" height="48" name="icon"/>
			  </FlashItem>
			  <Button skin="png.common.btn_cs2_close" x="264" y="6" var="closeBtn"/>
			</View>;
            return;
        }// end function

        override protected function createChildren() : void
        {
            createView(this.uiXML);
            this.init();
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            TweenVisibleManager.instance.register(this, TweenVisibleManager.Up, TweenVisibleManager.Right);
            return;
        }// end function

        public function init() : void
        {
            this.visible = false;
            this.addEventListener(MouseEvent.CLICK, this.openHandler);
            this.closeBtn.clickHandler = new Handler(this.close);
            this.x = GlobalLayer.instance.root.stage.stageWidth - 296 - 50;
            this.y = GlobalLayer.instance.root.stage.stageHeight - 163 - 100;
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            this.x = GlobalLayer.instance.root.stage.stageWidth - 296 - 50;
            this.y = GlobalLayer.instance.root.stage.stageHeight - 163 - 100;
            TweenVisibleManager.instance.register(this, TweenVisibleManager.Up, TweenVisibleManager.Right);
            return;
        }// end function

        public function show(param1:Object) : void
        {
            if (this._timeID > 0)
            {
                clearTimeout(this._timeID);
                this._timeID = 0;
            }
            TweenVisibleManager.instance.changevisible(this, true);
            this._data = param1;
            this._data.index = param1.index;
            this.item.data = ItemObject.create(param1[4]);
            if (param1[0] == 240202038)
            {
                this.item.mouseEnabled = false;
                this.item.mouseChildren = false;
            }
            else
            {
                this.item.mouseEnabled = true;
                this.item.mouseChildren = true;
            }
            this._isvip = false;
            if (this._data && this._data[4].cls == 2 && this._data[4].buffertype == 102)
            {
                this._isvip = true;
                this._readtime = 5;
                this.btn_VIP.label = "使用(" + this._readtime + ")";
                this.tips.url = "skill/vip.png";
                this.tips.x = 63;
                this._timeID = setInterval(this.timeHandler, 1000);
            }
            else if (this._data && int(this._data[4].itemtype) == 2 && int(this._data[4].cls) == 7 && int(this._data[4].autonopen) != 1)
            {
                this.btn_VIP.label = "开启宝箱";
                this.tips.url = "skill/baoxiang.png";
                this.tips.x = 44;
            }
            else
            {
                this.btn_VIP.label = "点击使用";
                this.tips.url = "skill/zuoqi.png";
                this.tips.x = 45;
            }
            return;
        }// end function

        private function timeHandler() : void
        {
            if (this._readtime > 0)
            {
                var _loc_1:String = this;
                var _loc_2:* = this._readtime - 1;
                _loc_1._readtime = _loc_2;
                this.btn_VIP.label = "使用(" + this._readtime + ")";
            }
            else
            {
                this.openHandler();
            }
            return;
        }// end function

        private function openHandler(event:MouseEvent = null) : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            var _loc_4:Point = null;
            var _loc_5:Object = null;
            var _loc_6:Array = null;
            if (this._timeID > 0)
            {
                this._readtime = 5;
                clearTimeout(this._timeID);
                this._timeID = 0;
            }
            if (this._isvip)
            {
                ModelProxy.change("plugin", "openBox", this._data);
            }
            else if (this.btn_VIP.label == "开启宝箱")
            {
                _loc_2 = UserData.getBagData();
                if (_loc_2[this._data.index][4])
                {
                    ModelProxy.change("plugin", "useitem", this._data);
                }
                else
                {
                    ModelProxy.change("movetips", "error", "该宝箱已被使用");
                }
            }
            else
            {
                _loc_3 = UserData.getUserData("level");
                if (_loc_3 < 80)
                {
                    WarningManager.show(30, [80, "时装系统"]);
                    this.close();
                    return;
                }
                ModelProxy.change("plugin", "useitem", this._data);
                if (this._data[4].link == "role#5#0")
                {
                    ModelProxy.change("data", "hdZuoqiId", this._data[4].itemid);
                }
                _loc_4 = UIGlobel.pointsDic["role"];
                _loc_5 = new Object();
                _loc_6 = (this._data[4].link as String).split("#");
                if (_loc_6[2])
                {
                    _loc_5.page = _loc_6[2];
                }
                PluginManager.instance.openPlugin("role", new Point(_loc_4.x, _loc_4.y), _loc_5);
            }
            this.close();
            return;
        }// end function

        public function close() : void
        {
            TweenVisibleManager.instance.changevisible(this, false);
            return;
        }// end function

    }
}
